<template>
  <div class="list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      class="list-container"
    >
       <!-- <van-card
        v-for="item in list"
        :key="item.id"
        :num="item.saleNum"
        :price="item.price"
        desc="描述信息"
        :title="item.title"
        :thumb="item.image"
      /> -->
      <div
        v-for="item in list"
        :key="item.id"
        class="list-item"
        is="router-link"
        :to="{name: 'detail', params: {id: item.id}}"
        tag="div"
      >
        <img :src="item.image" alt="">
        <span>{{item.title}}</span>
        <div>
          <span style="margin-right:8px">{{item.price}}</span>
          <span>{{item.saleNum}}人已购买</span>
        </div>
      </div>
    </van-list>
  </div>
</template>

<script>
import { getList } from '@/api/list'
export default {
  name: 'List',
  data() {
    return {
      list: [],
      loading: false, // 控制加载状态
      finished: false,
      page: 1
    }
  },
  methods: {
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      getList(this.$route.query.url, this.page).then(res => {
        console.log(res)
        // 将当前查询出的数据合并到已有的数组中
        this.list = this.list.concat(res)
        // 页面加1
        this.page += 1
        // 标记加载完成
        this.loading = false
        // 当加载新页面数据返回列表长度为0时，说明没有更多数据可以加载了
        if (res.length === 0) {
          this.finished = true
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.list {
    &-container {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    }
    &-item {
      width: 48%;
      margin: 6px 0;

      img {
        max-width: 100%;
      }
    }
  }
</style>
